package org.cn.osiris.basic.controller;

import org.cn.osiris.basic.domain.SmsCodeDto;
import org.cn.osiris.basic.dto.CodeDto;
import org.cn.osiris.basic.exception.BusinessException;
import org.cn.osiris.basic.service.IVerifyCodeService;
import org.cn.osiris.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    public AjaxResult imageVerifyCode(@PathVariable("key") String key){
        try {
            String base64Str = verifyCodeService.getImageVerifyCode(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (BusinessException e){
            //业务异常捕获
            return AjaxResult.me().setSuccess(false).setMessage("获取失败!"+e.getMessage());
        } catch (Exception e) {
            //500
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统异常,请稍后重试!");
        }
    }

    @PostMapping("/smsCode")
    public AjaxResult sendSmsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyCodeService.sendSmsCode(smsCodeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发送失败!"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统异常,请稍后重试!");
        }
    }
    /**
     * 微信接口绑定时发送的手机号
     *
     * */
    @PostMapping("/binderSmsCode")
    public AjaxResult binderVerifyCode(@RequestBody CodeDto codeDto){
        try {
            verifyCodeService.binderVerifyCode(codeDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            //后端打印错误日志
            //e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

}
